﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using Future.Settings;
using Future.Common;
using Future.Enums;

namespace Future.FormBase
{
    public partial class frmBaseDataDictionary : frmBaseDataForm
    {
        public frmBaseDataDictionary()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 表格内是否有数据，定义该方法用于派生类使用
        /// </summary>
        /// <returns></returns>
        public override bool HasData()
        {
            if (_SummaryView.GridControl.DataSource != null)
            {
                if (_SummaryView.GridControl.DataSource is DataTable)
                {
                    return ((DataTable)_SummaryView.GridControl.DataSource).Rows.Count > 0;
                }
                else
                    return false;
            }
            else
                return false;
        }

        #region 几个断言方法
        /// <summary>
        /// 检查对象是否为空
        /// </summary>
        /// <param name="obj">要检测的对象</param>
        /// <param name="errMsg">如果为空提示信息</param>
        protected void AssertNull(object obj, string errMsg)
        {
            if (null == obj) throw new Exception(errMsg);
        }

        /// <summary>
        /// 检查对象对等
        /// </summary>
        /// <param name="objA"></param>
        /// <param name="objB"></param>
        /// <param name="errMsg">如果不相等提示信息</param>
        protected void AssertEqual(object objA, object objB, string errMsg)
        {
            if (objA != null && objB != null)
            {
                if (!objA.Equals(objB))
                    throw new Exception(errMsg);
            }
        }
        /// <summary>
        /// 检查是否有选择一条记录.
        /// </summary>        
        protected void AssertFocusedRow()
        {
            bool ret = (_SummaryView == null) || (_SummaryView.FocusedRowHandle < 0);
            if (ret) throw new Exception("您没有选择记录，操作取消!");
        }

        /// <summary>
        /// 检查数据集是否有数据
        /// </summary>
        /// <param name="data"></param>
        protected void AssertRowNull(DataSet data)
        {
            bool ret = (data == null) || (data.Tables.Count == 0) || (data.Tables[0].Rows.Count <= 0);
            if (ret) throw new Exception("该数据集没有数据!");
        }

        /// <summary>
        /// 检查数据集是否有数据
        /// </summary>
        /// <param name="data"></param>
        protected void AssertTableEmpty(DataSet ds)
        {
            if ((ds == null) || (ds.Tables.Count <= 0) || ds.Tables[0].Rows.Count <= 0)
                throw new Exception("该数据集没有数据!");
        }

        #endregion
        /// <summary>
        /// 给表格绑定数据
        /// </summary>
        /// <param name="dataSource"></param>
        protected void DoBindingSummaryGrid(DataTable dataSource)
        {
            _SummaryView.GridControl.BeginInit();
            _SummaryView.GridControl.DataSource = dataSource;
            _SummaryView.GridControl.EndInit();
        }
        /// <summary>
        ///  绑定Summary外部导航条.
        /// </summary>
        /// <param name="navigator"></param>
        /// <param name="gc"></param>
        protected void BindingSummaryNavigator(ControlNavigator navigator, GridControl gc)
        {
            navigator.NavigatableControl = gc;
            navigator.ButtonClick += new NavigatorButtonClickEventHandler(OnSummaryNavigatorButtonClick);
        }

        /// <summary>
        /// 主表格导航按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSummaryNavigatorButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            try
            {
                Future.Common.CCursor.ShowWaitCursor();
                NavigatorButton btn = (NavigatorButton)e.Button;
                ControlNavigatorButtons buttons = ((ControlNavigator)sender).Buttons;
                if (e.Button == buttons.First) DoMoveFirst();
                if (e.Button == buttons.Prev) DoMovePrior();
                if (e.Button == buttons.Next) DoMoveNext();
                if (e.Button == buttons.Last) DoMoveLast();
            }
            finally
            {
                e.Handled = true;
                Future.Common.CCursor.ShowDefaultCursor();
            }
        }
        /// <summary>
        /// 双击表格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnGridViewDoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (SystemConfig.CurrentConfig == null) return;

                IButtonInfo btn = Buttons.GetButtonByName("btnEdit");

                if (SystemConfig.CurrentConfig.DoubleClickOnEditMode)
                {
                    this.DoEdit(btn);
                    return;
                }

                if (this.HasData())
                {
                    this.DoViewContent(btn);
                    SetDetailEditorsAccessable(_DetailGroupControl, false);
                }
                else
                {
                    Msg.ShowError("没有数据!");
                }
            }
            catch { }
        }
        /// <summary>
        /// 删除表格内指定行号的记录
        /// </summary>
        /// <param name="rowHandle"></param>
        protected virtual void DeleteSummaryRow(int rowHandle)
        {
            if (rowHandle >= 0)
            {
                if (_SummaryView.GridControl.DataSource is DataTable)
                {
                    DataTable dt = _SummaryView.GridControl.DataSource as DataTable;
                    dt.Rows.Remove(_SummaryView.GetDataRow(rowHandle));
                }
            }
        }
        /// <summary>
        /// 保存时会有此情况发生,往往不会更新最後一个编辑框里的数据.
        /// 当移除焦点後会更新数据.
        /// </summary>
        protected void UpdateLastControl()
        {
            try
            {
                if (ActiveControl == null) return;
                Control ctl = ActiveControl;
                txtFocusForSave.Focus();
                ActiveControl = ctl;
            }
            catch (Exception ex) { Msg.ShowException(ex); }
        }
        /// <summary>
        /// 保存单据後更新Summary当前行对象.实际为刷新数据行为.RowState=None;
        /// 如果是修改后保存,刷新当前记录的对象.
        /// 如果是新增后保存,在表格内插入一条记录.
        /// </summary>
        protected virtual void UpdateSummaryRow(DataRow dr)
        {
            if (dr == null) return;
            try
            {
                if (_SummaryView.GridControl.DataSource == null)
                {
                    _SummaryView.GridControl.DataSource = dr.Table.Clone();
                }

                DataTable dt = _SummaryView.GridControl.DataSource as DataTable;

                int focusedRowHandle = _SummaryView.FocusedRowHandle;

                if (_UpdateType == UpdateType.Add)
                {
                    DataRow newRow = dt.NewRow();
                    newRow.ItemArray = ((DataRow)dr).ItemArray;
                    dt.Rows.Add(newRow);
                    _SummaryView.FocusedRowHandle = dt.Rows.Count - 1;
                    dt.AcceptChanges();
                }

                if (_UpdateType == UpdateType.None || _UpdateType == UpdateType.Update)
                {
                    DataRow drU = _SummaryView.GetDataRow(focusedRowHandle);
                    drU.ItemArray = ((DataRow)dr).ItemArray;
                    drU.Table.AcceptChanges();
                    _SummaryView.RefreshRow(focusedRowHandle);//修改或新增要刷新Grid数据
                }

            }
            catch (Exception ex) { Msg.ShowException(ex); }
        }


        /// <summary>
        /// 因绑定控件的数据源为DataTable,所以提供SourceRow创建一个DataTable
        /// </summary>
        protected DataTable CreateDataBinder(DataRow sourceRow)
        {
            DataTable dt = sourceRow.Table.Clone();
            dt.Rows.Add(sourceRow.ItemArray);
            return dt;
        }

        /// <summary>
        /// 清空容器内输入框.
        /// </summary>
        public void ClearContainerEditorText(Control container)
        {
            for (int i = 0; i < container.Controls.Count; i++)
            {
                if (container.Controls[i] is TextEdit)
                    ((TextEdit)container.Controls[i]).EditValue = null;
                else if (container.Controls[i] is TextBoxBase)
                    ((TextBoxBase)container.Controls[i]).Clear();
            }
        }

        private void frmBaseDataDictionary_Activated(object sender, EventArgs e)
        {
            //无操作状态下不可输入数据
            if (_UpdateType != UpdateType.Add && _UpdateType != UpdateType.Update)
                SetDetailEditorsAccessable(_DetailGroupControl, false);
        }
    }
}
